import { createRouter, createWebHistory } from 'vue-router'
import PKindexViewView from '../views/pk/PKindexView.vue'
import RecordindexView from '../views/record/RecordindexView.vue'
import RanklistindexView from '../views/ranklist/RanklistindexView.vue'
import UserBotindexView from '../views/user/bot/UserBotindexView.vue'
import NotFound from '../views/error/NotFound.vue'
import UserAccountLoginView from '../views/user/account/UserAccountLoginView.vue'
import UserAccountRegisterView from '../views/user/account/UserAccountRegisterView.vue'
import store from '../store/index'

const routes = [
  {
    path:"/",
    name:"home",
    redirect:"/pk",
    meta:{
      requestAuth: true,
    }
  },
 {
  path:"/pk",
  name:"pk_index",
  component: PKindexViewView,
  meta:{
    requestAuth: true,
  }
 },
 {
  path:"/record",
  name:"record_index",
  component: RecordindexView,
  meta:{
    requestAuth: true,
  }
 },
 {
  path:"/ranklist",
  name:"randlist_index",
  component: RanklistindexView,
  meta:{
    requestAuth: true,
  }
 },
 {
  path:"/user/bot",
  name:"user_bot_index",
  component: UserBotindexView,
  meta:{
    requestAuth: true,
  }
 },
 {
  path:"/user/account/login",
  name:"user_account_login",
  component: UserAccountLoginView,
  meta:{
    requestAuth: false,
  }
 },
 {
  path:"/user/account/register",
  name:"user_account_register",
  component: UserAccountRegisterView,
  meta:{
    requestAuth: false,
  }
 },

 {
  path:"/404",
  name:"404",
  component: NotFound,
  meta:{
    requestAuth: false,
  }
 },
 {
    path:"/:catchAll(.*)",
    redirect:"/404"
 }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})


router.beforeEach((to, from, next) => {

  let flag = 1;
  const jwt_token = localStorage.getItem("jwt_token");

  if (jwt_token) {
    store.commit("updateToken", jwt_token);
    store.dispatch("getInfo", {
      success() {
      },
      error() {
        alert("token无效，请重新登录！");
        router.push({ name: 'userLogin' });
      }
    })
  } else {
    flag = 2;
  }

  if (to.meta.requestAuth && !store.state.user.is_login) {
    if (flag === 1) {
      next();
    } else {
      alert("请先进行登录！");
      next({name: "userLogin"});
    }
  } else {
    next();
  }
})



export default router
